#include <QFileDialog>

#include "newuserwindow.h"
#include "ui_newuserwindow.h"
#include "mainwindow.h"
#include "person.h"
#include "basewriter.h"



/**
 * @file newuserwindow.cpp
 * @author  Paweł Hanejko <phanejko@gmail.com>
 * @version 1.0
 *
 * @section LICENSE
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details at
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @section DESCRIPTION
 * Plik źródłowy zawiera implementację okna dodawania użytkowników do bazy
 *
 *
 */


NewUserWindow::NewUserWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::NewUserWindow)
{
    ui->setupUi(this);
}

NewUserWindow::~NewUserWindow()
{
    delete ui;
}


void NewUserWindow::on_addImage_clicked()
{
    files = QFileDialog::getOpenFileNames(this,
        tr("Add Image"), "/home", tr("Image Files (*.png *.jpg )"));
    ui->listWidget->addItems(files);
}

bool NewUserWindow::notValid()
{
    if(ui->lineEdit->text()=="" ) return true;

    if(ui->lineEdit_2->text()=="") return true;

    if(ui->lineEdit_3->text()=="") return true;

    if(ui->listWidget->count()==0) return true;

    return false;

}

void NewUserWindow::on_save_clicked()
{

    if (notValid())return;
    MainWindow* parent=(MainWindow*)this->parent();
    QStringList list;
    QString name;
    foreach(QString f,files)
    {
        name=f.split("/").last();
        list.push_back(name);
        if(! QFile::copy(f,name)) {
            ui->listWidget->clear();
            ///jakis dialog powinien wyskoczyc z informacja ze pliki sa uszkodzone;)
            return;
        }
    }
    BaseWriter w(parent->basefile);
    w.write(Person(ui->lineEdit_3->text(),ui->lineEdit_2->text(),ui->lineEdit->text(),list));
    parent->updateUsers();
    this->close();
    this->deleteLater();
}
